package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.mapper.BaseDictMapper;
import com.kaili.basic.mapper.BaseEmployeeMapper;
import com.kaili.basic.mapper.BasePdaMapper;
import com.kaili.basic.mapper.BasicSiteMapper;
import com.kaili.basic.pojo.dto.BasePdaAddDto;
import com.kaili.basic.pojo.dto.BasePdaQueryDto;
import com.kaili.basic.pojo.entity.BaseDict;
import com.kaili.basic.pojo.entity.BasePda;
import com.kaili.basic.pojo.entity.BasePdaExport;
import com.kaili.basic.pojo.entity.BasicSite;
import com.kaili.basic.service.BasePdaService;
import com.kaili.common.entity.BaseEmployee;
import com.kaili.common.util.LoginDetailUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by renzhikun on 18-6-21.
 */
@Service
public class BasePdaServiceImpl implements BasePdaService {

    @Autowired
    private BasePdaMapper basePdaMapper;
    @Autowired
    private BaseDictMapper dictMapper;
    @Autowired
    private BaseEmployeeMapper employeeMapper;
    @Autowired
    private BasicSiteMapper siteMapper;

    @Override
    public void addBasePda(BasePdaAddDto dto) {
        Integer uid = LoginDetailUtils.getCurrentEmployeeId();
        if (uid != null) {
            BasePda basePda = new BasePda();
            BeanUtils.copyProperties(dto,basePda);

            Date now = new Date();
            basePda.setCreateBy(uid.longValue());
            basePda.setCreateTime(now);
            basePda.setModifyBy(uid.longValue());
            basePda.setModifyTime(now);
            basePdaMapper.addBasePda(basePda);
        }
    }

    @Override
    public void deleteBasePda(Long pdaId) {
        Map<String, Object> map = new HashMap();
        Integer uid = LoginDetailUtils.getCurrentEmployeeId();
        if (uid != null) {
            map.put("modifyBy", uid.longValue());
            map.put("pdaId",pdaId);
            basePdaMapper.deleteBasePda(map);
        }
    }

    @Override
    public void updateBasePda(BasePdaAddDto dto) {
        Integer uid = LoginDetailUtils.getCurrentEmployeeId();
        if (uid != null) {
            BasePda basePda = new BasePda();
            BeanUtils.copyProperties(dto,basePda);

            basePda.setModifyBy(uid.longValue());
            basePdaMapper.updateBasePda(basePda);
        }
    }

    @Override
    public Page<BasePda>  queryBasePda(Page<BasePda> page, BasePdaQueryDto dto) {
        List<BasePda> list = basePdaMapper.queryBasePda(page, dto);
        return page.setRecords(list);
    }

    @Override
    public List<String> selectBySn(String pdaSn) {
        return basePdaMapper.selectBySn(pdaSn);
    }

    @Override
    public List<String> veriBySn(String pdaSn) {
        return basePdaMapper.veriBySn(pdaSn);
    }

    @Override
    public BasePda getBySn(String pdaSn) {
        return basePdaMapper.getBySn(pdaSn);
    }

    @Override
    public List<BasePdaExport> queryPdaExport(Map<String, Object> map) {
        return basePdaMapper.queryPdaExport(map);
    }

    @Override
    @Transactional
    public List<Long> doImPort(List<BasePdaExport> org) {
        List<Long> ids= new ArrayList<>();
        BasePda basePda = new BasePda();
        for (BasePdaExport pda:org) {
            basePda.setPdaSn(pda.getPdaSn());

            BaseDict firm = dictMapper.selectByDictName(pda.getFirmName());
            Long firmId = Long.valueOf(firm.getDictId());
            basePda.setFirmId(firmId);

            BaseDict device = dictMapper.selectByDictName(pda.getDeviceName());
            Long deviceId = Long.valueOf(device.getDictId());
            basePda.setDeviceId(deviceId);
            BasicSite bs = siteMapper.querySiteName(pda.getSite());
            Long siteId;
            if (bs != null){
                siteId = bs.getSiteId();
                basePda.setSiteId(siteId);
            }

            BaseEmployee be = employeeMapper.getEmpByName(pda.getEmployee());
            Long employeeId ;
            if (be !=null){
                employeeId = Long.valueOf(be.getId());
                basePda.setEmployeeId(employeeId);
            }

            basePda.setTelephone(pda.getTelephone());
            basePda.setMacAddress(pda.getMacAddress());
            if (!StringUtils.isEmpty(pda.getEnable())) {
                basePda.setEnable(pda.getEnable());
            }else {
                basePda.setEnable("0");
            }
            basePda.setRemark(pda.getRemark());
            basePda.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
            basePda.setCreateTime(new Date());
            basePda.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
            basePda.setModifyTime(new Date());
            BasePda result = basePdaMapper.selectPdaByParams(basePda);

            if(null!=result){
                basePdaMapper.updateBasePda(result);
            }else{
                basePdaMapper.addBasePda(basePda);
            }
        }
        return ids;

    }
}
